package vn.aptech.leaveManagementSystem.view.admin;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import vn.aptech.leaveManagementSystem.business.dto.Employee;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;
import vn.aptech.leaveManagementSystem.dao.DBManager;
import javax.swing.JButton;

import com.toedter.calendar.JDateChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateUser extends JFrame{
	DBManager con = new DBManager();
	Connection conn;
	String sql;
	ResultSet rs;
	
	
	JPanel pnlMain;
	private JTextField txtFirstname;
	private JTextField txtLastname;
	private JTextField txtPassword;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private JTextArea txtAddress;
	private JDateChooser txtBirthday;
	private JDateChooser txtJoindate;
	private JButton btnCancel;
	JFrame thisFrame = this;
	
	
	public UpdateUser() {
		pnlMain = new JPanel();
		pnlMain.setLayout(null);
		
		
		
		getContentPane().add(pnlMain);
		
		JLabel lblNewLabel = new JLabel("ID :");
		lblNewLabel.setBounds(10, 30, 27, 14);
		pnlMain.add(lblNewLabel);
		
		JLabel lblFirst = new JLabel("First name :");
		lblFirst.setBounds(10, 55, 69, 14);
		pnlMain.add(lblFirst);
		
		JLabel lblLastName = new JLabel("Last name :");
		lblLastName.setBounds(10, 80, 69, 14);
		pnlMain.add(lblLastName);
		
		JLabel lblPassWord = new JLabel("Password :");
		lblPassWord.setBounds(10, 342, 69, 14);
		pnlMain.add(lblPassWord);
		
		JLabel lblPhone = new JLabel("Phone :");
		lblPhone.setBounds(10, 244, 69, 14);
		pnlMain.add(lblPhone);
		
		JLabel lblAddress = new JLabel("Address :");
		lblAddress.setBounds(10, 155, 69, 14);
		pnlMain.add(lblAddress);
		
		JLabel lblEmail = new JLabel("Email :");
		lblEmail.setBounds(10, 219, 69, 14);
		pnlMain.add(lblEmail);
		
		JLabel lblPosition = new JLabel("Position :");
		lblPosition.setBounds(10, 292, 69, 14);
		pnlMain.add(lblPosition);
		
		JLabel lblDepartment = new JLabel("Department :");
		lblDepartment.setBounds(10, 268, 69, 14);
		pnlMain.add(lblDepartment);
		
		String strGen[] = {"Female","Male"};
		final JComboBox cbbGender = new JComboBox(strGen);
		cbbGender.setBounds(89, 102, 181, 20);
		pnlMain.add(cbbGender);
		
		String strPos[] = {"Admin","Director","Manager","Employee"};
		final JComboBox cbbPosition = new JComboBox(strPos);
		cbbPosition.setBounds(89, 289, 181, 20);
		pnlMain.add(cbbPosition);
		
		String strDepartment[] = {"Business Department","Technical Department","HR Department"};
		final JComboBox cbbDepartment = new JComboBox(strDepartment);
		cbbDepartment.setBounds(89, 265, 181, 20);
		pnlMain.add(cbbDepartment);
		
		final JComboBox cbbID = new JComboBox();
		cbbID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sql = "select * from [Employee] where IDEmp = ?";
				try {
					conn = con.getConn();
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, cbbID.getSelectedItem().toString());
					ResultSet rs = ps.executeQuery();
					
					while(rs.next())
		            {
		                txtFirstname.setText(rs.getString(2).trim());
		                txtLastname.setText(rs.getString(3).trim());
		                cbbGender.setSelectedIndex(rs.getInt(4));
		                txtBirthday.setDate(rs.getDate(5));
		                txtAddress.setText(rs.getString(6).trim());
		                txtEmail.setText(rs.getString(7));
		                txtPhone.setText(rs.getString(8).trim());
		                cbbDepartment.setSelectedIndex(rs.getInt(9)-1);
		                cbbPosition.setSelectedIndex(rs.getInt(10)-1);
		                txtJoindate.setDate(rs.getDate(11));
		                //txtPassword.setText(rs.getString(13).trim());
		                /*this.cmbPosition.setSelectedIndex(rs.getInt(3)-1);
		                this.cmbDepartment.setSelectedIndex(rs.getInt(4)-1);
		                this.cmbSeniority.setSelectedIndex(rs.getInt(5)-1);
		                this.txtPhone.setText(rs.getString(7).trim());
		                this.txtAddress.setText(rs.getString(8).trim());
		                this.txtEmail.setText(rs.getString(9).trim());
		                this.txtPass.setText(rs.getString(11).trim());*/                
		                
		                
		                
		            }
		            rs.close();
		            conn.close();
					//ps.setString(1, )
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		cbbID.setBounds(94, 27, 176, 20);
		pnlMain.add(cbbID);
		
		txtFirstname = new JTextField();
		txtFirstname.setBounds(90, 52, 274, 20);
		pnlMain.add(txtFirstname);
		txtFirstname.setColumns(10);
		
		txtLastname = new JTextField();
		txtLastname.setBounds(89, 77, 275, 20);
		pnlMain.add(txtLastname);
		txtLastname.setColumns(10);
		
		/*txtPassword = new JTextField();
		txtPassword.setBounds(89, 339, 275, 20);
		pnlMain.add(txtPassword);
		txtPassword.setColumns(10);*/
		
		txtPhone = new JTextField();
		txtPhone.setBounds(88, 241, 276, 20);
		pnlMain.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtAddress = new JTextArea();
		
		JScrollPane scrollpane = new JScrollPane(txtAddress);
		scrollpane.setBounds(89, 150, 275, 62);
		pnlMain.add(scrollpane);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(89, 216, 275, 20);
		pnlMain.add(txtEmail);
		txtEmail.setColumns(10);
		
		
		
		
		
		JButton btnChange = new JButton("CHANGE");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sql = "update [Employee] set FirstName = ?, LastName = ?,Gender = ?, Birthday = ?, [Address] = ?, Email = ?, Phone = ?, DepartmentID = ?,PositionID = ?,JoinDate = ?,[Password] =? where IDEmp = ?";
				try
	            {
					Integer id = Integer.parseInt(cbbID.getSelectedItem().toString());
					String firstname = txtFirstname.getText().trim();
					String lastname = txtLastname.getText().trim();
					String gender = cbbGender.getSelectedItem().toString(); 
					//Date birthday = (Date)txtBirthday.getDate();
					Date birthday =  MyParse.toSqlDate(txtBirthday.getDate());
					String address = txtAddress.getText().trim();
					String email = txtEmail.getText().trim();
					String phone = txtPhone.getText().trim();
					//((JTextField)txtJoindate.getDateEditor().getUiComponent()).getText();
					String department = cbbDepartment.getSelectedItem().toString();
					String position = cbbPosition.getSelectedItem().toString();
					//Date joindate = (Date) txtJoindate.getDate();
					Date joindate = MyParse.toSqlDate(txtJoindate.getDate());
					//String password = txtPassword.getText().trim();
					//String status = "Single";
					
					
					
int departmentID = 1;
		            
		            if(department.equals("Technical Department"))
		            {
		                departmentID = 2;    
		            }
		            else if(department.equals("HR Department"))
		            {
		                departmentID = 3;
		            }
		           /* else if(department.equals("Accounting"))
		            {
		                departmentID = 4;
		            }
		            else if(department.equals("Sale"))
		            {
		                departmentID = 5;
		            }
		            else if(department.equals("Marketing"))
		            {
		                departmentID = 6;
		            }*/
		            boolean Gender = false;
		            if (gender.equals("Male")) {
						Gender = true;
					}
		            else Gender = false;
		            
		           /* boolean StatusEmp = false;
		            if (status.equals("Single")) {
						StatusEmp = true;
					}else StatusEmp = false;*/
		            
		            int positionID = 1;
		            
		            if(position.equals("Director"))
		            {
		                positionID = 2;
		            }
		            else if(position.equals("Manager"))
		            {
		                positionID = 3;
		            }
		            else if(position.equals("Employee"))
		            {
		                positionID = 4;
		            }
		            		           
		            
		            if(firstname.equals(""))
		            {
		                JOptionPane.showInputDialog(this, "Please enter firstname");   
		            }
		            else if(lastname.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Lastname");
		            }   
		            else if(birthday.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Birthday"); 
		            }
		            else if(address.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Address"); 
		            }
		            else if(email.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Email"); 
		            }
		            else if(!email.matches("[a-zA-z]+[a-zA-Z0-9]*@[a-zA-Z]+[.][a-zA-Z]+"))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Email is not correct, Please try again !");
		            }
		            else if(phone.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Number Phone"); 
		            }    
		            else if(joindate.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Joindate"); 
		            }
		           /* else if(password.equals(""))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Please enter Password"); 
		            }
		            else if(!password.matches("[a-zA-Z0-9]{6,12}"))
		            {
		                JOptionPane.showMessageDialog(thisFrame,"Password is not correct, Please try again !");
		            }*/
		          
		           
					
					
					
	                conn = con.getConn();
	                //PreparedStatement ps = conn.prepareStatement(sql);
	                Employee emp = new Employee();
	               // emp.setIDEmp(id);
	                emp.setFirstName(firstname);
	                emp.setLastName(lastname);
	                emp.setGender(Gender);
	                /*if (gender == "Male") {
	                	emp.setGender(true);
		            }else emp.setGender(false);*/
	                emp.setBirthday(birthday);
	                emp.setAddress(address);
	                emp.setEmail(email);
	                emp.setPhone(phone);
	                emp.setDepartmentID(departmentID);             
	                emp.setPositionID(positionID);
	                emp.setJoinDate(joindate);
	                //emp.setStatusEmp(StatusEmp);
	              /*  if (status == "Single") {
	                	emp.setStatusEmp(true);
		            }else emp.setStatusEmp(false);*/
	                
	                MyModel mymodel = new MyModel();
					mymodel.employees.updateEmployee(emp); //mymodel.employees.addEmployee(emp) tra ve return cs.executeUpdate();
					
						JOptionPane.showMessageDialog(thisFrame, "Update Account suseccful");
						thisFrame.setVisible(false);
						AdminForm admin = new AdminForm();
						//JOptionPane.showMessageDialog(thisFrame, "Update Account Unsuccessful, Please try again");
						// txtPassword.setText("");
                     
	                 conn.close();
	            } 
	            catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
		});
		
		btnChange.setBounds(129, 370, 89, 23);
		pnlMain.add(btnChange);
		
		btnCancel = new JButton("CANCEL");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminForm adminForm =  new AdminForm();
				adminForm.setVisible(true);
			}
		});
		btnCancel.setBounds(228, 370, 89, 23);
		pnlMain.add(btnCancel);
		
		JLabel lblGender = new JLabel("Gender :");
		lblGender.setBounds(10, 105, 46, 14);
		pnlMain.add(lblGender);
		
		
		
		JLabel lblBirthday = new JLabel("Birthday :");
		lblBirthday.setBounds(10, 130, 69, 14);
		pnlMain.add(lblBirthday);
		
		txtBirthday = new JDateChooser();
		txtBirthday.setBounds(89, 127, 275, 20);
		pnlMain.add(txtBirthday);
		
		JLabel lblJoinDate = new JLabel("Join date :");
		lblJoinDate.setBounds(10, 317, 69, 14);
		pnlMain.add(lblJoinDate);
		
		txtJoindate = new JDateChooser();
		txtJoindate.setBounds(89, 314, 275, 20);
		pnlMain.add(txtJoindate);
		

		//get Employee details
		sql = "select * from Employee";
		Statement st;
		try {
			conn = con.getConn();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				cbbID.addItem(rs.getString(1));
			}
			rs.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		setVisible(true);
		setTitle("Information User");
		setSize(390, 569);
		setLocationRelativeTo(null);
	}	
}


/*pnlMain = new JPanel();
pnlMain.setLayout(null);



add(pnlMain);
setVisible(true);
setTitle("Information User");
setSize(390, 569);
setLocationRelativeTo(null);*/
